<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%--获取流程待办和日程--%>
<%
    JSONObject jsonData = new JSONObject();
    User user = HrmUserVarify.getUser( request, response );
    int userid = user.getUID();
    int docount = getMyListDoing(userid);
    int reqcount = getMyListRequest(userid);
    int workcount = getWorkplan(userid);
    int mailcount = getMailCount(userid);
    jsonData.put( "docount", docount );
    jsonData.put( "reqcount", reqcount );
    jsonData.put( "workcount", workcount );
    jsonData.put( "mailcount", mailcount );
    out.print( jsonData.toJSONString() );
%>

<%!

    public static int getMyListDoing(int userid){
        RecordSet rs = new RecordSet();
        String sql = "select  count(*)  from workflow_requestbase t1,workflow_currentoperator t2,workflow_base \n" +
                "                t3 where t1.requestid = t2.requestid and t1.workflowid=t3.id and \n" +
                "                t2.userid in ("+userid+") and t2.usertype=0 and (isnull(t1.currentstatus,-1) = -1 \n" +
                "    or (isnull(t1.currentstatus,-1)=0 and t1.creater in ("+userid+")))  \n" +
                "                and (t1.deleted<>1 or t1.deleted is null or t1.deleted='')  \n" +
                "                and ((t2.isremark='0' and (t2.takisremark is null or t2.takisremark=0 )) \n" +
                "                or t2.isremark in('1','5','8','9','7','11')) and t2.islasttimes=1 and t1.currentnodetype<3 and \n" +
                "                (isprocessing = '' or isprocessing is null)  and t3.isvalid in('1','3')";
        rs.executeQuery(sql);
        rs.next();
        return Util.getIntValue(rs.getString(1));
    }

    public static int getMyListRequest(int userid){
        RecordSet rs = new RecordSet();
        String sql = "select  count(*)  from workflow_requestbase t1,workflow_currentoperator t2,\n" +
                "                workflow_base t3 where t1.requestid = t2.requestid and t1.workflowid=t3.id and t1.workflowid !=1 \n" +
                "                and t2.userid in ("+userid+") and t2.usertype=0 and (isnull(t1.currentstatus,-1) = -1 \n" +
                "                or (isnull(t1.currentstatus,-1)=0 and t1.creater in ("+userid+")))   and (t1.deleted<>1 \n" +
                "                or t1.deleted is null or t1.deleted='')  and t1.creater in ("+userid+") and t1.creatertype = 0 \n" +
                "                and t1.creater = t2.userid  and ((t1.currentnodetype <> '3') \n" +
                "                or (t2.isremark in('1','2','4','5','8','9','11') and t1.currentnodetype = '3' )) \n" +
                "                and t2.islasttimes=1  and t3.isvalid in('1','3') and t1.currentnodetype <> '3'";
        rs.executeQuery(sql);
        rs.next();
        return Util.getIntValue(rs.getString(1));

    }

    public static int getWorkplan(int userid){
        String date1 = DateUtil.getFirstDayOfWeek();
        String date2 = DateUtil.getLastDayOfWeek();
        RecordSet rs = new RecordSet();
        String sql = "select count(*) from (\n" +
                "select distinct t1.id from WorkPlan t1 join WorkPlanShareDetail t2 on t1.id=t2.workid where T1.createrid="+userid+" AND T2.userid="+userid+" AND\n" +
                " (  (  (T1.endDate > '"+date1+"' or ( T1.endDate = '"+date1+"' and T1.endtime >= '00:00' ) )  \n" +
                "and ( T1.beginDate < '"+date2+"' or ( T1.beginDate = '"+date2+"' and T1.beginTime <='23:59' ))  )  \n" +
                "or (  (len(T1.endDate)=0  AND ( T1.beginDate > '"+date1+"' or ( T1.beginDate = '"+date1+"' and T1.beginTime >='00:00' )) ) ))\n" +
                ") A";
        rs.executeQuery(sql);
        rs.next();
        return Util.getIntValue(rs.getString(1));
    }

    public static int getMailCount(int userid){
        RecordSet rs = new RecordSet();
        String sql = "select id,resourceid,subject,senddate from (\n" +
                "select id,resourceid,subject,senddate from mailresource  \n" +
                "where resourceid='"+userid+"' and status='0' order by senddate desc)";
        rs.executeQuery( sql);
        rs.next();
       return rs.getCounts();
    }
%>




